package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    /**
     * 套餐查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 假设我们有一个方法可以获取到套餐列表
        Page<Setmeal> page = setMealDao.findPage(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 新增套餐
     * @param setmeal
     */
    @Transactional
    @Override
    public void save(Setmeal setmeal,Integer[] checkgroupIds) {
        setMealDao.save(setmeal);

        //获取套餐的id
        Integer setmealId = setmeal.getId();

        //添加到套餐关系表
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.addCheckGroup(setmealId,checkgroupId );
        }
    }

    /**
     * 根据图片名字统计
     * @param img
     * @return
     */
    @Override
    public long countByImg(String img) {
        return setMealDao.countByImg(img);
    }

    /**
     * 体检预约_套餐展示
     * @return
     */
    @Override
    public List<Setmeal> selectGetSetmeal() {
        return setMealDao.selectGetSetmeal();
    }

    /**
     * 套餐详情查看
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        return setMealDao.findById(id);
    }
}